<%@page isELIgnored="false" pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
    <script type="text/javascript">
        $(function () {
            $('#ssCoupon').searchbox({
                searcher:function(value,name){
                    $("#tableCoupon").datagrid("load",{
                        "columnName":name,
                        "columnValue":value,
                    });
                },
                width:300,
                menu:'#mmCoupon',
                prompt:'请输入值',
            });

            $("#tableCoupon").datagrid({
                url: "${pageContext.request.contextPath}/back/coupon/findAll",
                autoRowHeight: true,
                fit:true,
                fitColumns: true,
                toolbar: '#tbCoupon',
                columns: [[
                    {field: "checkbox", checkbox: true},
                    {title: "名称", field: "name", width: 100},
                    {title: "额度(单位：元)", field: "quota", width: 100},
                    {title: "消费门槛(单位：元)", field: "sill", width: 100},
                    {title: "有效期(单位：天)", field: "validity", width: 100},
                    {title: "数量(单位：张)", field: "count", width: 100},
                    {title: "最高限抵运费折扣率(单位：%)", field: "discount", width: 100},
                    {
                        title: "操作", field: "options", width: 100, formatter: function (value, row, index) {
                            return "<a onclick=showCoupon(\""+row.id+"\") class='btnCoupon' data-options=\"iconCls:'icon-edit',\">更新</a>";
                        }
                    }
                ]],
                pagination: true,
                checkOnSelect: true,
                selectOnCheck: true,
                pageList:[5,10,20,40,80,100],
                pageSize:5,
                onLoadSuccess: function () {
                    $(".btnCoupon").linkbutton();
                }
            });
        });

        //打开添加对话框
        function openAddCouponDialog() {
            $("#addCouponDialog").dialog({
                title:"添加优惠券信息",
                iconCls:'icon-save',
                width:600,
                height:400,
                modal:true,
                href:"${pageContext.request.contextPath}/back/coupon/add.jsp",
                buttons:[{
                    text:'保存',
                    iconCls:'icon-save',
                    handler:savecoupon,
                },{
                    text:'关闭',
                    iconCls:'icon-cancel',
                    handler:function(){
                        $("#addCouponDialog").dialog("close");
                    }
                }]
            });
        }
        //保存优惠券信息
        function savecoupon(){
            $('#saveCouponForm').form({
                url:'${pageContext.request.contextPath}/back/coupon/add',
                onSubmit: function(){
                        return $("#saveCouponForm").form("validate");
                },
                success:function(result){
                    result = eval("("+ result +")");
                    $.messager.show({
                        title:'提示信息',
                        msg:result.message,
                        timeout:5000,
                        showType:'slide'
                    });
                    $('#addCouponDialog').dialog("close");
                    $("#tableCoupon").datagrid("reload");
                }
            });
            $('#saveCouponForm').submit();
        }
        //更新优惠券信息
        function updateCoupon(){
            $('#updateCouponForm').form({
                url:'${pageContext.request.contextPath}/back/coupon/update',
                onSubmit: function(){
                        return $("#updateCouponForm").form("validate");
                },
                success:function(result){
                    result = eval("("+ result +")");
                    $.messager.show({
                        title:'提示信息',
                        msg:result.message,
                        timeout:5000,
                        showType:'slide'
                    });
                    $('#updateCouponDialog').dialog("close");
                    $("#tableCoupon").datagrid("reload");
                }
            });
            $('#updateCouponForm').submit();
        }
        //展示优惠券信息
        function showCoupon(id){

            $("#updateCouponDialog").dialog({
                title:"更新优惠券信息",
                iconCls:'icon-edit',
                width:600,
                height:400,
                modal:true,
                href:"${pageContext.request.contextPath}/back/coupon/update.jsp",
                buttons:[{
                    text:'保存',
                    iconCls:'icon-save',
                    handler:updateCoupon,
                },{
                    text:'关闭',
                    iconCls:'icon-cancel',
                    handler:function(){
                        $("#updateCouponDialog").dialog("close");
                    }
                }],
                onLoad:function(){
                    $("#updateCouponForm").form('load','${pageContext.request.contextPath}/back/coupon/findOne?id='+id);
                }
            });
        }
    </script>
<table id="tableCoupon" class="easyui-datagird"></table>

<div id="tbCoupon">
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true,onClick:openAddCouponDialog,">添加</a>

    <input id="ssCoupon"></input>
    <div id="mmCoupon" style="width:120px">
        <div data-options="name:'name',iconCls:'icon-ok'">名称</div>
        <div data-options="name:'quota',iconCls:'icon-ok'">额度</div>
        <div data-options="name:'sill',iconCls:'icon-ok'">消费门槛</div>
        <div data-options="name:'validity',iconCls:'icon-ok'">有效期</div>
        <div data-options="name:'count',iconCls:'icon-ok'">数量</div>
        <div data-options="name:'discount',iconCls:'icon-ok'">最高限抵运费折扣率</div>
    </div>
</div>

<%--添加对话框--%>
<div id="addCouponDialog"></div>
<%--更新对话框--%>
<div id="updateCouponDialog"></div>
